package org.novaacropole.util;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;

import org.apache.commons.lang.exception.ExceptionUtils;

import com.sun.faces.application.ActionListenerImpl;

public class FacesActionListener extends ActionListenerImpl {

	@Override public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
		try {
			DAO.begin();
			super.processAction(actionEvent);
			FacesUtil.next(actionEvent);
			DAO.commit();
		} catch (Throwable t) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(ExceptionUtils.getRootCause(t).getMessage()));
			DAO.rollback();
		} finally {
			DAO.close();
		}
	}
}
